package ssm.com.controler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import ssm.com.pojo.Door;
import ssm.com.pojo.Order;
import ssm.com.service.DoorService;
import ssm.com.service.OrderService;
import ssm.com.utils.tOfTime;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

@Controller
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private DoorService doorService;

    //查询全部数据
    @RequestMapping("findAllOrderInfo")
    public String findAllOrderInfo(HttpServletRequest request){
        List<Order> list = orderService.findAllOrderInfo();
        request.setAttribute("orderList",list);

        return "order";
    }

    //根据id删除数据
    @RequestMapping("deleteOrderById")
    public String deleteOrderById(Integer id, HttpServletResponse response) throws IOException {
        int row = orderService.deleteOrderById(id);
        if (row<=0) {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter writer = response.getWriter();
            writer.println("<script>alert('删除失败');location.href='findAllOrderInfo';</script>");
            writer.close();
        }
        //重定向
        return "redirect:findAllOrderInfo";
    }

    //获取添加页面
    @RequestMapping("toAdd")
    public String toAdd(HttpServletRequest request){
        List<Door> list = doorService.findAllDoorInfo();
        request.setAttribute("doorList",list);
        return "addOrder";
    }
    //添加
    @RequestMapping("add")
    public String add(Order order,String createTime1,String endTime1){
        tOfTime tOfTime = new tOfTime();
        Date createTime = tOfTime.removeT(createTime1);
        Date endTime = tOfTime.removeT(endTime1);

        order.setCreateTime(createTime);
        order.setEndTime(endTime);
        //System.out.println(order);
        int row = orderService.add(order);
        return "redirect:findAllOrderInfo";
    }

    //获取更新页面
    @RequestMapping("toUpdate")
    public String toUpdate(Integer did,Integer oid,HttpServletRequest request){
        Door door = doorService.findDoorByIdUpdate(did);
        //获取店名
        String doorName = door.getDoorName();
        request.setAttribute("doorName",doorName);

        List<Door> list = doorService.findAllDoorInfo();
        request.setAttribute("doorList",list);

        Order order = orderService.findOrderById(oid);
        request.setAttribute("order",order);
        return "updateOrder";
    }
    //更新
    @RequestMapping("doUpdate")
    public String doUpdate(Order order){
        //System.out.println(order);
        //System.out.println();
        int row = orderService.doUpdate(order);
        return "redirect:findAllOrderInfo";
    }

    //分页显示
    @RequestMapping("findOrderLimit")
    public String findOrderLimit(@RequestParam(name="currentPage",defaultValue = "1") Integer currentPage,
                                 HttpServletRequest request){
        if (currentPage<=0) {
            currentPage = 1;
        }
        //页数
        Integer offSet = 2;
        int count = orderService.getCount();
        Integer pages = count/offSet;
        Integer yuShu = count%offSet;
        if (yuShu>0) {
            pages++;
        }
        if (currentPage>pages) {
            currentPage=pages;
        }


        Integer pageNum = (currentPage-1)*offSet;
        request.setAttribute("currentPage",currentPage);
        request.setAttribute("pages",pages);
        request.setAttribute("count",count);

        List<Order> list = orderService.findOrderLimit(pageNum,offSet);
        request.setAttribute("orderList",list);



        return "order";
    }

}
